package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.SendRegularlyMallsMapper;
import com.iretailer.report.mapper.SendRegularlyMapper;
import com.iretailer.report.mapper.SendRegularyLogMapper;
import com.iretailer.report.model.SendRegularly;
import com.iretailer.report.model.SendRegularlyLog;
import com.iretailer.report.model.SendRegularlyMalls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/5/30 10:50
 * @Description:
 */
@Service
public class SendRegularlyService extends ServiceImpl<SendRegularlyMapper, SendRegularly> {

    @Autowired
    private SendRegularlyMapper sendRegularlyMapper;
    @Autowired
    private SendRegularlyMallsMapper sendRegularlyMallsMapper;
    @Autowired
    private SendRegularyLogMapper sendRegularyLogMapper;
    @Autowired
    private CompanyService companyService;

    public PageResult<SendRegularly> grid(String companyId, Integer enabled, String companyName, QueryOption queryOption) {
        Page page = new Page<SendRegularly>(1, Long.MAX_VALUE);
        QueryWrapper<SendRegularly> queryWrapper = new QueryWrapper<>();
        if (enabled != null) {
            queryWrapper.eq("enabled", enabled);
        }
        if (companyName != null) {
            queryWrapper.like("companyName", companyName);
        }
        if (companyId != null) {
            List<String> childrens = companyService.getTreeId(companyId);
            childrens.add(companyId);
            queryWrapper.in("companyId", childrens);
        }
        queryOption.judge(page, queryWrapper);
        IPage pageInfo = sendRegularlyMapper.selectPageSendRegularly(page, queryWrapper);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords());
    }

    public PageResult<SendRegularlyLog> gridLogs(Long sendRegularlyId, QueryOption queryOption) {
        Page page = new Page<SendRegularly>(1, Long.MAX_VALUE);
        QueryWrapper<SendRegularlyLog> queryWrapper = new QueryWrapper<>();
        if (sendRegularlyId != null) {
            queryWrapper.eq("sendRegularlyId", sendRegularlyId);
        }
        queryWrapper.orderByDesc("CreateTime");
        queryOption.judge(page, queryWrapper);
        IPage pageInfo = sendRegularyLogMapper.selectPageSendRegularlyLog(page, queryWrapper);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords());
    }

    public List<SendRegularly> selectSendRegularly(QueryWrapper queryWrapper) {
        return sendRegularlyMapper.selectSendRegularly(queryWrapper);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteByIds(List<String> ids) {
        // 删除关联的案场
        sendRegularlyMallsMapper.delete(new QueryWrapper<SendRegularlyMalls>().lambda().in(SendRegularlyMalls::getSendregularyId, ids));
        // 删除日志
        sendRegularyLogMapper.delete(new QueryWrapper<SendRegularlyLog>().lambda().in(SendRegularlyLog::getSendregularlyId, ids));
        // 删除 SendRegularly
        sendRegularlyMapper.deleteBatchIds(ids);
        return true;
    }

    /**
     * 添加和修改
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean saveOrUpdate(SendRegularly insert, String userName) {
        insert.setUpdateUser(userName);
        insert.setUpdateTime(LocalDateTime.now());
        if (insert.getId() != null && insert.getId() != 0) {
            sendRegularlyMapper.updateById(insert);
        } else {
            sendRegularlyMapper.insert(insert);
        }
        return true;
    }


    @Transactional(propagation = Propagation.REQUIRED)
    public boolean insertSendRegularlyMalls(Long sendregularyId, List<String> mallIds) {
        if (sendregularyId != null && sendregularyId != 0 && mallIds != null) {
            sendRegularlyMallsMapper.delete(new QueryWrapper<SendRegularlyMalls>().lambda().eq(SendRegularlyMalls::getSendregularyId, sendregularyId));
            sendRegularlyMallsMapper.insertSendregularyIdMalls(sendregularyId, mallIds);
            sendRegularlyMapper.updateMallNumber(sendregularyId, mallIds.size());
            return true;
        } else {
            return false;
        }
    }

    public List<SendRegularlyMalls> getSendRegularlyMalls(Long sendregularyId) {
        return sendRegularlyMallsMapper.selectList(new QueryWrapper<SendRegularlyMalls>().lambda().eq(SendRegularlyMalls::getSendregularyId, sendregularyId));
    }

    /***
     * 根据 sendRegularyId 查询最后一次的日志
     * @param sendRegularyId
     * @return
     */
    public SendRegularlyLog getLatelyLog(Long sendRegularyId) {
        return sendRegularyLogMapper.selectOne(new QueryWrapper<SendRegularlyLog>().lambda().eq(SendRegularlyLog::getSendregularlyId, sendRegularyId).orderByDesc(SendRegularlyLog::getCreateTime).last("limit 1"));
    }

    /**
     * 更新日志和状态
     *
     * @param log
     */
    public void updateSendRegularlyMessage(SendRegularlyLog log) {
        sendRegularlyMapper.updateById(new SendRegularly(log.getSendregularlyId(), log.getCreateTime(), log.getStatus()));
        sendRegularyLogMapper.insert(log);
    }
}
